//
//  LoginView.m
//  iOSBaseFramework
//
//  Created by user on 2020/6/29.
//  Copyright © 2020 com.taylor. All rights reserved.
//

#import "LoginView.h"
#import "UIView+TRExtension.h"

#import "Masonry/Masonry/Masonry.h"

@interface LoginView () <UITextFieldDelegate>



#pragma mark - textfield接口实现
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField;        // return NO to disallow editing.
- (void)textFieldDidBeginEditing:(UITextField *)textField;           // became first responder
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField;          // return YES to allow editing to stop and to resign first responder status. NO to disallow the editing session to end
- (void)textFieldDidEndEditing:(UITextField *)textField;             // may be called if forced even if shouldEndEditing returns NO (e.g. view removed from window) or endEditing:YES called
- (void)textFieldDidEndEditing:(UITextField *)textField reason:(UITextFieldDidEndEditingReason)reason API_AVAILABLE(ios(10.0)); // if implemented, called in place of textFieldDidEndEditing:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;   // return NO to not change text

- (void)textFieldDidChangeSelection:(UITextField *)textField API_AVAILABLE(ios(13.0), tvos(13.0));

- (BOOL)textFieldShouldClear:(UITextField *)textField;               // called when clear button pressed. return NO to ignore (no notifications)
- (BOOL)textFieldShouldReturn:(UITextField *)textField;              // called when 'return' key pressed. return NO to ignore.
@end


@implementation LoginView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

-(instancetype)init {
    NSLog(@"fuck init");
    if (self = [super init]) {
        CGRect rect = [[UIScreen mainScreen] bounds];
        _keyboardOffsetY = 0;
        _textFieldWidth = rect.size.width - 100;
        
        self.frame = [[UIScreen mainScreen] bounds];
        self.backgroundColor = [UIColor whiteColor];
        
//       _textField = [[UITextField alloc] initWithFrame:CGRectMake(50, CGRectGetMaxY(rect) - 100, CGRectGetWidth(rect)-100, 50)];
        _textField = [[TRTextField alloc] initWithFrame:CGRectMake(50, CGRectGetMinY(rect) + 100, _textFieldWidth, 50)];
        _textField.backgroundColor = [UIColor grayColor];
        _textField.clearButtonMode = UITextFieldViewModeWhileEditing;
        _textField.borderStyle = UITextBorderStyleRoundedRect;
//        _textField.placeholder = @"请输入手机号码";
        _textField.text = @"";
        _textField.textColor = [UIColor blackColor];
        _textField.font = [UIFont systemFontOfSize:25];
        _textField.textAlignment = NSTextAlignmentLeft;
        _textField.adjustsFontSizeToFitWidth = NO;
        
        _textField.returnKeyType = UIReturnKeyDone;
        NSMutableAttributedString *attributed = [[NSMutableAttributedString alloc] initWithString:@"请输入手机号码"];
        [attributed addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(0, [@"请输入手机号码" length])];
        _textField.attributedPlaceholder = (NSAttributedString*)attributed;
        _textField.delegate = self;
        [self addSubview:_textField];
        
        _areaLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(rect) - 100, 50, 50)];
        [_areaLabel setTextAlignment:NSTextAlignmentCenter];
        [_areaLabel setText:@"+86"];
        [_areaLabel setFont:[UIFont systemFontOfSize:20]];
        [self addSubview:_areaLabel];
        
        _secertTf = [[UITextField alloc] initWithFrame:CGRectMake(50, CGRectGetMinY(rect) +70, CGRectGetWidth(rect)-100, 50)];
        _secertTf.backgroundColor = [UIColor grayColor];
        _secertTf.secureTextEntry = true;
        _secertTf.adjustsFontSizeToFitWidth = true;
        _secertTf.textAlignment = NSTextAlignmentLeft;
        _secertTf.borderStyle = UITextBorderStyleRoundedRect;
        _secertTf.clearButtonMode = UITextFieldViewModeWhileEditing;
        
        _secertTf.text = @"";
        _secertTf.placeholder = @"请输入密码";
        _secertTf.delegate = self;
        [self addSubview:_secertTf];
        
        _forgotSecertBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_forgotSecertBtn setTitle:@"忘记密码" forState:UIControlStateNormal];
        [_forgotSecertBtn setTitleColor:[UIColor blueColor] forState:UIControlStateSelected];
        [self addSubview:_forgotSecertBtn];
        //        [_forgotSecertBtn setY:CGRectGetMinY(rect) + 70];
//        [_forgotSecertBtn setX:rect.size.width - 100];
//        [self addSubview:_forgotSecertBtn];
            
        _registerBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_registerBtn setTitle:@"注册" forState:UIControlStateNormal];
        [_registerBtn setTitleColor:[UIColor blueColor] forState:UIControlStateSelected];
        [self addSubview:_registerBtn];
        
        [self.secertTf setRightView:self.forgotSecertBtn];
        [self.secertTf setRightViewMode:UITextFieldViewModeAlways];
        
        _footDescLabel = [[UILabel alloc] initWithFrame:rect];
        [_footDescLabel setText:@"还没有注册？"];
        [_footDescLabel setTextColor:[UIColor grayColor]];
        [_footDescLabel setX:rect.size.width/2 - 50];
        [_footDescLabel setY:CGRectGetMaxY(rect) - 40];
        [self addSubview:_footDescLabel];
       
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHideShow:) name:UIKeyboardWillHideNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHideShow:) name:UIKeyboardDidHideNotification object:nil];
    }
    return self;
}

#pragma mark -- 键盘处理
-(void)keyboardWillShow:(NSNotification *)notification {
    NSLog(@"will show keyboard");
     NSValue* aValue = [[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey];
     CGRect keyboardRect = [aValue CGRectValue];
     CGRect keyboardFrame = [self convertRect:keyboardRect fromView:[[UIApplication sharedApplication] keyWindow]];
     CGFloat keyboardHeight = keyboardFrame.origin.y;
     
     if (keyboardHeight < self.textField.y) {
         _keyboardOffsetY = self.textField.y - keyboardHeight + self.textField.height;
         self.y = self.y - _keyboardOffsetY;
     }
}

-(void)keyboardDidShow:(NSNotification *)notification {
    NSLog(@"have show keyboard");
}

-(void)keyboardWillHideShow:(NSNotification *)notification  {
    NSLog(@"will hide show");
    NSValue* aValue = [[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey];
   CGRect keyboardRect = [aValue CGRectValue];
   CGRect keyboardFrame = [self convertRect:keyboardRect fromView:[[UIApplication sharedApplication] keyWindow]];
//   CGFloat keyboardHeight = keyboardFrame.origin.y;
   
   if (_keyboardOffsetY > 0) {
       self.y = self.y + _keyboardOffsetY;
       _keyboardOffsetY = 0;
   }
}

-(void)keyboardDidHideShow:(NSNotification *)notification  {
    NSLog(@"have hide show");
}


#pragma mark -- 输入处理
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    NSLog(@" do textFieldShouldBeginEditing [shoud began]");
    return YES;
}

-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    NSLog(@"do textFieldDidBeginEditing [did began]");
}

-(BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    NSLog(@"do textFieldShouldEndEditing [should end]");
    return YES;
}

-(void)textFieldDidEndEditing:(UITextField *)textField
{
    NSLog(@"do textFieldDidEndEditing [did end]");
}

- (void)textFieldDidEndEditing:(UITextField *)textField reason:(UITextFieldDidEndEditingReason)reason
{
    NSLog(@"do textFieldDidEndEditing:reason:  [did end] %ld", reason );
//    self layoutCon
}

-(void)textFieldDidChangeSelection:(UITextField *)textField {
    NSLog(@"do textFieldDidChangeSelection [didchangeSelection]");
    NSLog(@"文字的真实长度:%ld,  内宽:%f 编辑区域宽:%f", self.textField.trueLength, self.textField.intrinsicContentSize.width, self.textField.minimumFontSize);
    [self layoutCountryOrArea];
    
}

- (BOOL)textFieldShouldClear:(UITextField *)textField {
    NSLog(@"do textFieldShouldClear [should clear]");
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    NSLog(@"do textFieldShouldReturn [should return]");
    [textField resignFirstResponder];
    return YES;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSLog(@"do textField:shouldChangeCharactersInRange:replacementString");
    return YES;
}

#pragma mark -- 触摸选择.size.width - 100];
        

-(void)layoutCountryOrArea {
    CGSize size = [self.textField intrinsicContentSize];

    CGFloat tmpWidth;
    if (size.width > self.textField.width * (1 - 1/7)) {
        tmpWidth = self.textField.width + 10;
        if (tmpWidth > ([UIScreen mainScreen].bounds.size.width - 50)) {
            tmpWidth = [UIScreen mainScreen].bounds.size.width - 50;
        }
    } else {
        tmpWidth = self.textField.width - 10;
        if (tmpWidth <= CGRectGetWidth([UIScreen mainScreen].bounds)-100) {
            tmpWidth = CGRectGetWidth([UIScreen mainScreen].bounds)-100;
        }
    }

    if (tmpWidth != self.textField.width) {

        self.areaLabel.width += self.textField.width - tmpWidth;
        self.areaLabel.width = (self.areaLabel.width < 10) ? 10 : self.areaLabel.width;
        self.areaLabel.width = (self.areaLabel.width > 50) ? 50 : self.areaLabel.width;

        _textFieldWidth = tmpWidth;
        
        [self.textField mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(_textFieldWidth);
            make.left.mas_equalTo(self.mas_left).offset(SCREEN_WIDTH - _textFieldWidth);
        }];
        [self.areaLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(SCREEN_WIDTH - _textFieldWidth);
        }];
    }
}


-(void)layoutSubviews {
    [super layoutSubviews];
    NSLog(@" 执行 layoutSubviews:");
    [self.textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(_textFieldWidth);
        make.height.mas_equalTo(50);
        make.left.mas_equalTo(self.mas_left).offset(100);
        make.top.mas_equalTo(self.mas_top).offset(100);
    }];
    
    [self.areaLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(self.width - _textFieldWidth);
        make.height.mas_equalTo(50);
        make.left.mas_equalTo(self.mas_left);
        make.top.mas_equalTo(self.textField.mas_top);
    }];
    
    [self.secertTf mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(self.width);
        make.height.mas_equalTo(50);
        make.left.mas_equalTo(0);
        make.top.equalTo(self.textField.mas_bottom);
    }];
    
    [self.forgotSecertBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.secertTf.mas_right).offset(-25);
        make.centerY.mas_equalTo(self.secertTf.mas_centerY);
    }];
//
//    [self.registerBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.right.mas_equalTo(self.mas_right).offset(-50);
//        make.top.mas_equalTo(self.secertTf.mas_bottom).offset(20);
//    }];
    
    [self.footDescLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.secertTf.mas_bottom).offset(20);
        make.left.mas_equalTo(self.secertTf.mas_left).offset(20);
    }];
    
    [self.registerBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.footDescLabel.mas_top);
        make.left.mas_equalTo(self.footDescLabel.mas_right).offset(2);
    }];
//
}

@end
